package com.streamkar.util;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.streamkar.entity.domain.Account;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName UserLocalCache
 * @Description
 * @Author tyfrost
 * @Date 2018/12/4 5:39 PM
 * @Version 1.0
 */
public class AccountLocalCache {
    private static final Logger LOGGER = LoggerFactory.getLogger(AccountLocalCache.class);

    private final static Cache<Integer, Account> cache = CacheBuilder.newBuilder().initialCapacity(100)
            .concurrencyLevel(5).expireAfterWrite(30, TimeUnit.MINUTES).build();

    public static void putCache(Integer key, Account account) {
        LOGGER.info("save Account into cache,the key is:{}", key);
        cache.put(key, account);
    }

    public static Account getCache(Integer key) {
        LOGGER.info("begin get Account from cache,the key is:{}", key);
        Account account = cache.getIfPresent(key);
        if (null == account) {
            LOGGER.info("get Account from cache,but not hit cache");
        }
        return account;
    }
}
